﻿using AutoMapper;
using DailyApp.API.ApiReponses;
using DailyApp.API.DataModel;
using DailyApp.API.DTOs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DailyApp.API.Controllers
{
    /// <summary>
    /// 备忘录接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MemoController : ControllerBase
    {
        /// <summary>
        /// 数据库上下文
        /// </summary>
        private readonly DailyDbContext db;

        /// <summary>
        /// AutoMapper自动映射器 (DTO 转为 数据模型)
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        ///  通过构造函数取出数据的上下文
        /// </summary>
        /// <param name="_db"></param>
        public MemoController(DailyDbContext _db, IMapper _mapper)
        {
            db = _db;
            mapper = _mapper;
        }
        /// <summary>
        /// 统计备忘录总数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult StatMemo(int? AccountId)
        { 
            ApiResponse res = new ApiResponse();

            try
            {
                var list = db.MemoInfo.Where(a => a.AccountId == AccountId).ToList();
                int count = list.Count();//where A.AccountId == AccountId

                res.ResultCode = 1;
                res.Msg = "查询正确";
                res.ResultData = count;
            }
            catch (Exception)
            {
                res.ResultCode = -99;
                res.Msg = "服务器忙,请稍后...";
            }
            return Ok(res);
        }
        /// <summary>
        /// 添加备忘录
        /// </summary>
        /// <param name="memoDTO">备忘录信息</param>
        /// <returns>1:添加成功,-1:失败,-99:异常</returns>
        [HttpPost]
        public IActionResult AddMemo(MemoDTO memoDTO)
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                //DTO转为数据模型
                //DTO数据传输用的模型，把数据模型和数据传输对象彻底分开，DTO可进行数据验证
                MemoInfo memoInfo = mapper.Map<MemoInfo>(memoDTO);
                db.MemoInfo.Add(memoInfo);
                int result = db.SaveChanges();
                if (result==1)
                {
                    //写info日志
                    apiResponse.ResultCode = 1;
                    apiResponse.Msg = "备忘录添加成功";
                }
                else
                {
                    //写debug日志
                    apiResponse.ResultCode = -1;
                    apiResponse.Msg = "备忘录添加失败";
                }
                

            }
            catch (Exception)
            {
                apiResponse.ResultCode = -99;
                apiResponse.Msg = "服务器忙,请稍后...";
            }
            return Ok(apiResponse);
        }
        /// <summary>
        /// 备忘录查询
        /// </summary>
        /// <param name="title">标题(模糊查询)</param>
        /// <returns>1:查询成功,-99:异常</returns>
        [HttpGet]
        public IActionResult QueryMemo(int? AccountId, string? title)
        { 
            ApiResponse apiResponse=new ApiResponse();

            try
            {
                var query = from A in db.MemoInfo.OrderByDescending(A => A.CreateTime)
                            where A.AccountId == AccountId
                            select new MemoDTO {
                                MemoId = A.MemoId,
                                Title = A.Title,
                                Content = A.Content,
                            };
                if (!string.IsNullOrEmpty(title))
                { 
                    query = query.Where(x => x.Title.Contains(title));
                }
                apiResponse.ResultCode = 1;
                apiResponse.Msg = "查询成功";
                apiResponse.ResultData = query;

            }
            catch (Exception)
            {
                apiResponse.ResultCode = -99;
                apiResponse.Msg = "服务器忙,请稍后...";
            }
            return Ok(apiResponse);
        }
        /// <summary>
        /// 编辑备忘录信息
        /// </summary>
        /// <param name="memoDTO">备忘录新信息</param>
        /// <returns>1:编辑成功,-1:失败,-2:ID传错了,-99:异常</returns>
        [HttpPut]
        public IActionResult EditMemo(MemoDTO memoDTO)
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
                //DTO转为数据模型
                //DTO数据传输用的模型，把数据模型和数据传输对象彻底分开，DTO可进行数据验证
                var dbInfo = db.MemoInfo.Find(memoDTO.MemoId);
                if (dbInfo == null)
                {
                    apiResponse.ResultCode = -2;
                    apiResponse.Msg = "请正确传备忘录ID";
                    return Ok(apiResponse);
                }

                //也可用Map转化
                dbInfo.Title = memoDTO.Title;
                dbInfo.Content = memoDTO.Content;

                int result = db.SaveChanges();
                if (result == 1)
                {
                    //写info日志
                    apiResponse.ResultCode = 1;
                    apiResponse.Msg = "备忘录编辑成功";
                }
                else
                {
                    //写debug日志
                    apiResponse.ResultCode = -1;
                    apiResponse.Msg = "备忘录编辑失败";
                }


            }
            catch (Exception)
            {
                apiResponse.ResultCode = -99;
                apiResponse.Msg = "服务器忙,请稍后...";
            }
            return Ok(apiResponse);
        }
        /// <summary>
        /// 删除备忘录
        /// </summary>
        /// <param name="memoId">备忘录ID</param>
        /// <returns>1:删除成功,-1:删除失败,-2:ID传错了,-99:异常</returns>
        [HttpDelete]
        public IActionResult DelMemo(int memoId)
        {
            ApiResponse apiResponse = new ApiResponse();
            try
            {
              
                var dbInfo = db.MemoInfo.Find(memoId);
                if (dbInfo == null)
                {
                    apiResponse.ResultCode = -2;
                    apiResponse.Msg = "请正确传备忘录ID";
                    return Ok(apiResponse);
                }

                db.MemoInfo.Remove(dbInfo);

                int result = db.SaveChanges();
                if (result == 1)
                {
                    //写info日志
                    apiResponse.ResultCode = 1;
                    apiResponse.Msg = "备忘录删除成功";
                }
                else
                {
                    //写debug日志
                    apiResponse.ResultCode = -1;
                    apiResponse.Msg = "备忘录删除失败";
                }


            }
            catch (Exception)
            {
                apiResponse.ResultCode = -99;
                apiResponse.Msg = "服务器忙,请稍后...";
            }
            return Ok(apiResponse);
        }

    }
}
